﻿using System;
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.AspNet.SignalR;

namespace GuideAPI
{

    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public class AuthorizeClaimsAttribute:AuthorizeAttribute
    {
        protected override bool UserAuthorized(IPrincipal user)
        {
            if (user==null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var principal = user as ClaimsPrincipal;

            if (principal!=null)
            {
                Claim authenticated = principal.FindFirst(ClaimTypes.Authentication);
                if (authenticated!=null&&authenticated.Value=="true")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}